package com.inspur.incubator.controller;

import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.inspur.incubator.common.tools.EncoderMd5;
import com.inspur.incubator.data.MakerQueryParam;
import com.inspur.incubator.data.Menu;
import com.inspur.incubator.data.MhUser;
import com.inspur.incubator.data.Role;
import com.inspur.incubator.data.Users;
import com.inspur.incubator.data.zTreeNode;
import com.inspur.incubator.service.RoleService;
import com.inspur.incubator.service.impl.MhUserImpl;

@Controller
@RequestMapping("/role")
public class CdRoleController {
	@Autowired
	RoleService roleService;
	@Autowired
	private MhUserImpl mhUserImpl;
	EncoderMd5 md5 = new EncoderMd5();
	
	@RequestMapping("/currentInfo")
	@ResponseBody
	public Map<String, Object> getCurrentInfo(@RequestParam(value="account")String account){
	    account = account.trim();
	    return roleService.getCurrentInfo(account);
	}
	
	@RequestMapping(value = "/account_manage")
	public ModelAndView getDataCenter_index() {
		return new ModelAndView("System/account_manage");
	}
	@RequestMapping(value ="/account_add")
	public ModelAndView account_add(){
		return new ModelAndView("System/account_add");
	}
	@RequestMapping(value = "/account_rolemanage")
	public ModelAndView account_rolemanage(){
		return new ModelAndView("System/account_rolemanage");
	}
	@RequestMapping(value = "/account_userAction")
	public ModelAndView account_userAction(){
		return new ModelAndView("System/account_userAction");
	}
	@RequestMapping(value = "/my_account")
	public ModelAndView My_account(){
		return new ModelAndView("System/my_account");
	}
	@RequestMapping(value = "/addUser")
	@ResponseBody
	public String addUser(MhUser mu){
		mu = mhUserImpl.findUser(mu);
		if (mu!=null) {
		    roleService.addUser(mu);
		}
		return "";
	}
	@RequestMapping(value = "/addRole")
	@ResponseBody
	public String addRole(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		String role_name= request.getParameter("role_name");
		String role_detail=request.getParameter("role_detail");
		Map<String,String> map = new HashMap<String,String>();
		map.put("role_name", role_name);
		map.put("role_detail", role_detail);
		int state =roleService.addRole(map);
		if(state == 1){
			return "success";
		}
		if(state == 0){
			return "repeact";	//已经有账号
		}
		return "error";
	}
	
	//通过条件筛选展示用户
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "showUser")
	@ResponseBody
	public String showUser(HttpServletRequest request,HttpServletResponse response){
		String name = URLDecoder.decode(URLDecoder.decode(request.getParameter("name")));
		String role = request.getParameter("role");
		String sdate = request.getParameter("sdate");
		String edate = request.getParameter("edate");
		String state = request.getParameter("state");
		int page = Integer.parseInt(request.getParameter("page"));
		int pagesize = Integer.parseInt(request.getParameter("pagesize"));
		int minrownum = (page-1) *pagesize+1; 	//计算要显示第几行到第几行
		int maxrownum = (page-1) *pagesize+8;	
		Map map = new HashMap();
		map.put("name", name);
		map.put("role", role);
		map.put("sdate", sdate);
		map.put("edate", edate);
		if(state.equals("all")){
			state ="";
		}
		map.put("state", state);
		map.put("maxrownum", maxrownum);
		map.put("minrownum", minrownum);
		System.out.println("后台接收查询参数:"+map.toString());
		Map resultMap = new HashMap();
		List<Map> columns = new ArrayList();
		int totalnum = roleService.getTabletotalnum(map);
		columns = roleService.getTable(map);
		resultMap.put("rows", columns);
    	resultMap.put("totalnum", totalnum);
    	return JSONObject.fromObject(resultMap).toString();
	}
	//更新用户的密码
	@RequestMapping(value = "/updatePass")
	@ResponseBody
	public String updatePass(HttpServletRequest request,HttpServletResponse response){
		String account =request.getParameter("account");
		String oldpass = request.getParameter("oldpass");
		String newpass = request.getParameter("newpass");
		System.out.println("account:"+account+",oldpass:"+oldpass+",newpass:"+newpass);
		return roleService.updatePass(account, oldpass, newpass);
	}
	//重置密码
	@RequestMapping(value = "/resetPass")
	@ResponseBody
	public String resetPass(HttpServletRequest request,HttpServletResponse response){
		String newpass = request.getParameter("newpass");
		String account = request.getParameter("account");
		System.out.println("newpass:"+newpass+",account:"+account);
		return roleService.resetPass(newpass,account);
	}
	//注销启用账号
	@RequestMapping(value = "/cancelAccount")
	@ResponseBody
	public int cancelAccount(HttpServletRequest request,HttpServletResponse response){
		String account = request.getParameter("account");
		String status = request.getParameter("state");
		return roleService.cancelAccount(account,status);
	}
	//查询账号信息
	@RequestMapping(value = "/showAccount")
	public ModelAndView showAccount(HttpServletRequest request,HttpServletResponse response){
		String account = request.getParameter("account");
		Users users = roleService.showAccount(account);
		ModelAndView modelAndView = new ModelAndView("System/account_update");
		modelAndView.addObject("users",users);
		modelAndView.hasView();
		return modelAndView;
	}
	//更新账号信息
	@RequestMapping(value = "/updateAccount")
	@ResponseBody
	public String updateAccount(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		String account = request.getParameter("account");
		String role_id = request.getParameter("role_id");
		String name = request.getParameter("name");
		String role_name =request.getParameter("role_name");
		Map map = new HashMap();
		map.put("account", account);
		map.put("name", name);
		map.put("role_id", role_id);
		map.put("role_name", role_name);
		int state = roleService.updateAccount(map);
		if(state != 0){
			return "success";
		}
		return "error";
	}
		//查询角色
		@RequestMapping(value = "/getRoles")
		@ResponseBody
		public List<Role> getRoles(){
			Map map = null;
			return roleService.getRoles(map);
		}
	   //查询权限
		@RequestMapping(value = "/showRoleMenu")
		@ResponseBody
		public List<zTreeNode> showRoleMenu(HttpServletRequest request,HttpServletResponse response) {
			String role_id = request.getParameter("role_id");
			List<zTreeNode> menus = roleService.getMenus(role_id); 
			return menus;
		}
		//修改权限
		@RequestMapping(value = "/insertRoleMenu")
		@ResponseBody
		public void insertRoleMenu(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			String role_id = request.getParameter("role_id");
			String nodes = request.getParameter("nodes");
		    JSONArray myJsonArray = JSONArray.fromObject(nodes);
		    Collection collection= JSONArray.toCollection(myJsonArray,zTreeNode.class);
			List<zTreeNode> menus=new ArrayList<zTreeNode>();
	        Iterator it = collection.iterator();
	        while (it.hasNext()) {
	        	zTreeNode user = (zTreeNode) it.next();
	        	menus.add(user);
	        }
	        roleService.insertRoleMenu(menus,role_id);
		}
}
